package com.zqxq.lottery.api.alipay;


import com.zqxq.lottery.api.alipay.util.AlipayNotify;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

public class AlipayNotityValidator {

    public static boolean notityValidator(Map<String,String> map){

        String mstr2 = mapToQueryString(map, false);
        System.out.println(mstr2);

        String rr = AlipayNotify.verifyResponse(map.get("notify_id"));
        /*if(rr.trim().equals("true")){
            String user_id = "0";
            if(map.get("passback_params")!=null){
                String[] arr = map.get("passback_params").toString().split("-");
                user_id = arr[0];
            }
            String trade_status = map.get("trade_status");
            String total_amount = map.get("total_amount");
            String out_trade_no = map.get("out_trade_no");
            String refund_fee = map.get("refund_fee");
            System.out.println(trade_status+","+user_id+","+total_amount+","+out_trade_no+","+refund_fee);
            if("TRADE_SUCCESS".equals(trade_status)){
//                JqbNetClient.send_jqb_recharge(user_id,total_amount,out_trade_no,refund_fee);
                return true;
            }
        }*/
        String trade_status = map.get("trade_status");
        if("TRADE_SUCCESS".equals(trade_status)){
//                JqbNetClient.send_jqb_recharge(user_id,total_amount,out_trade_no,refund_fee);
            return true;
        }
        return false;
    }

    private static String mapToQueryString(Map<String, String> map, boolean encode) {
        StringBuilder string = new StringBuilder();
        if (map != null && !map.isEmpty()) {
            try {
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    string.append(entry.getKey());
                    string.append("=");
                    string.append(encode ? URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8")
                            : String.valueOf(entry.getValue()));
                    string.append("&");
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return string.toString();
    }
}
